package com.topscomm.handler;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationListener;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import com.google.common.base.Converter;
import com.topscomm.cbo.service.ICboMessageAppService;
import com.topscomm.cbo.service.ICboMessageSysService;
import com.topscomm.mms.pojo.MmsUserAccessInfoEntity;
import com.topscomm.mms.service.impl.MmsUserAccessInfoService;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.util.UserAgentUtil;
import com.topscomm.tap.common.ParameterCache;
import com.topscomm.tap.common.SessionUserBean;
import com.topscomm.tap.event.TapLoginSuccessEvent;
import com.topscomm.tap.security.SecurityUserDetails;
import com.topscomm.tap.threadlocal.ThreadLocalContext;

/**
 * @description:登录成功监听，业务系统可写类似的监听器监听
 * @author: zhanglongfei
 * @date: 2020年4月24日上午11:04:25
 * @modify:
 */
@Component
public class MmsLoginSuccessEventHandler implements ApplicationListener<TapLoginSuccessEvent> {
	@Autowired
	HttpServletRequest httpServletRequest;
	@Autowired
	private MmsUserAccessInfoService mmsUserAccessInfoService;
	@Override
	public void onApplicationEvent(TapLoginSuccessEvent event) {
		if (event.getExternalData()==null) {
			addUserAccessInfo(event.getUserDetails());
		}

	}
	/**
	 * 插入用户访问信息表
	 */
	public void addUserAccessInfo(UserDetails userDetails) {
		MmsUserAccessInfoEntity userAccessInfoEntity = new MmsUserAccessInfoEntity();
		SecurityUserDetails securityUserDetails = (SecurityUserDetails)userDetails;
		userAccessInfoEntity.setUsercode(securityUserDetails.getUser().getCode());
		userAccessInfoEntity.setUsername(securityUserDetails.getUser().getName());
		userAccessInfoEntity.setDeptname(securityUserDetails.getDept().getFullname());
		userAccessInfoEntity.setOrgname(securityUserDetails.getOrg().getName());
		userAccessInfoEntity.setOrgcode(securityUserDetails.getOrg().getCode());
		userAccessInfoEntity.setDeptcode(securityUserDetails.getDept().getCode());
		userAccessInfoEntity.setAccesstime(new Date());
		String requestUrl = httpServletRequest.getRequestURI();
		userAccessInfoEntity.setAccessurl(requestUrl);
		//获取登录方式
		String loginmode = "PC";
		String deviceType = httpServletRequest.getParameter("deviceType");
		if(!StringUtil.isEmpty(deviceType)) {
			loginmode = deviceType;
		}
		userAccessInfoEntity.setLoginmode(loginmode);
		// 获取User-Agent信息
		String userAgentString = httpServletRequest.getHeader("User-Agent");
		//获取浏览器信息
		String browserInfo = UserAgentUtil.getBrowserName(userAgentString) + UserAgentUtil.getBrowserVersion(userAgentString);
		userAccessInfoEntity.setAccessbrowser(browserInfo);
		userAccessInfoEntity.setAccesstime(new Date());
		userAccessInfoEntity.setAccessip(BaseUtil.getIPAddress(httpServletRequest));
		mmsUserAccessInfoService.insert(userAccessInfoEntity);
	}
}
